<?php

require_once './libs/facebook-php-sdk/src/facebook.php';
require_once './src/DBClass.php';
require_once './src/ErrorClass.php';

class LoginClass {
  var $facebook;
  
  public function LoginClass() {
    $this->facebook = new Facebook(array(
      'appId'  => '289014377844877',
      'secret' => 'c2cc1f1504948c0a3cc51736b591e005'
    ));
  }

  public function isLogged() {
    return true;
/*
    $id = $this->getUserId();
    if ($id <> 0) {
      $db = new DBClass();
      return $db->isIdInDB($id);
    } else {
      return false;
    }
*/
  }
    
  public function getUserId() {
    return $this->facebook->getUser();
  }
  
  public function getLoginUrl() {
    return $this->facebook->getLoginUrl();
  }
  
  public function getUserProfile() {
    try {
      return $this->facebook->api('/me');
    } catch (FacebookApiException $e) {
      return null;
    }
  }
  
  public function registerUser() {
    $id = $this->getUserId();
    if ($id <> 0) {
      $db = new DBClass();
      return $db->writeUserId($id);
    } else {
      return new ErrorClass(NOT_LOGGED);
    }
  }
}
?>
